//入口文件
import Vue from 'vue'
//导入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'



//导入时间插件
import moment from 'moment'
//定义全局过滤器
Vue.filter('dataFormat',function(dataStr,pattern = "YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
})

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
// Vue.http.options.root = 'http://localhost:8888';//设置请求跟路径
//导入vuepreview组件
import VuePreview from 'vue-preview'
// Vue.use(VuePreview)
Vue.use(VuePreview, {
    mainClass: 'pswp--minimal--dark',
    barsSize: {top: 0, bottom: 0},
    captionEl: false,
    fullscreenEl: false,
    shareEl: false,
    bgOpacity: 0.85,
    tapToClose: true,
    tapToToggleControls: false
})

//导入mui的样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

//按需导入mint-ui中组件
import "mint-ui/lib/style.css"

import {Header,Swipe,SwipeItem,Button,Lazyload,Switch} from 'mint-ui'
Vue.component(Header.name,Header)
Vue.component(Swipe.name,Swipe)
Vue.component(SwipeItem.name,SwipeItem)
Vue.component(Button.name,Button)
Vue.use(Lazyload)
Vue.component(Switch.name, Switch);

import Vuex from 'vuex'
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem("car") || "[]");
var store = new Vuex.Store({
    state:{
        car:car
    },
    mutations:{
        addToCar(state,goodsinfo){
            // console.log(goodsinfo)
            var flag = false;
            state.car.some(item => {
                if(item.id == goodsinfo.id){
                    // item.count =parseInt(item.count) + parseInt(goodsinfo.count);
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true;
                }
            });
            if(!flag){
                state.car.push(goodsinfo);
            }
            //每次调用添加购物车时，将数据存入localstorage
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateCount(state,goodsinfo){
            // console.log(goodsinfo)
            state.car.some(item => {
                if(item.id == goodsinfo.id){
                    if(!!goodsinfo.count){
                        item.count = goodsinfo.count;
                    }
                    if(!!goodsinfo.selected){
                        item.selected = goodsinfo.selected
                    }
                    return true;
                }
            });
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        remove(state,id){
            // state.car.some(item,index => {
            //     if(item.id == id){
            //         state.car.delete(index);
            //     }
            // });
            state.car.splice(id,1);
            localStorage.setItem("car",JSON.stringify(state.car));
        }
    },
    getters:{
        getAllCount:function(state){
            var c = 0;
            state.car.forEach(item => {
                c += parseInt(item.count);
            })
            return c;
        },
        getCountJson:function(state){
            var j = {};
            state.car.forEach(item => {
                j[item.id] = item.count;
            });
            return j;
        },
        getAllPrice:function(state){
            var c = 0,p = 0;
            state.car.forEach(item =>{
                if(item.selected){
                    c += parseInt(item.count)
                    p += parseInt(item.price * item.count)
                }
            });
            return {count:c,price:p};
        }
    }
})


import app from './App.vue'

var vm = new Vue({
    el:'#app',
    render:c => c(app),
    router,
    store
})